package cn.cloud9.activiti7.gateway;

import cn.cloud9.activiti7.ActivitiBase;
import cn.cloud9.activiti7.var.Evection;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author OnCloud9
 * @description
 * @project activiti7
 * @date 2022年08月21日 上午 10:25
 */
public class ExclusiveGatewayDemo extends ActivitiBase {

    @Test
    public void deploymentDemo() {
        final Deployment deploy = repositoryService.createDeployment()
                .name("出差流程申请-排他网关案例")
                .addClasspathResource("./bpmn/evection-exclusive-gateway.bpmn20.xml")
                .deploy();

        final String id = deploy.getId();
        final String name = deploy.getName();
        System.out.println("id = " + id);
        System.out.println("name = " + name);
    }

    @Test
    public void startProcess() {
        final Evection evection = new Evection();
        evection.setDays(3D);
        final Map<String, Object> processVarMap = new HashMap<>();

        // 设置全局变量和流程变量
        // 对象形式的流程变量必须要实现序列化接口
        processVarMap.put("evection", evection);

        final ProcessInstance instance = runtimeService
                .startProcessInstanceByKey("evection-exclusive-gateway", processVarMap);
    }

    /**
     *
     * 完成前置任务
     */
    @Test
    public void completePreviewTask() {
        final TaskQuery taskQuery = taskService.createTaskQuery();
        String[] assigns = {"张三", "李四"};
        for (String assign : assigns) {
            final Task task = taskQuery
                    .processDefinitionKey("evection-exclusive-gateway")
                    .taskAssignee(assign)
                    .singleResult();
            taskService.complete(task.getId());
        }
    }
}
